(in-package :ws-sql)

(defparameter *wsdl-output-directory* 
  "/home/evenson/net/logos/work/ws/src/"
  "Directory on filesytem where the results of introspection will be saved.")

(defun initialize ()
  (unless *metadata*
    (warn "No database metadata in ws-sql::*metadata* special variable.")
    (database-initialize)
    (possibly-refresh-metadata)))

(defun introspect-catalog (&key catalog (schema "EUIDB"))
    (multiple-value-bind (wsdl xsd)
	(transcribe-wsdl 
	 (mklist 
	  (get-procedures :catalog catalog :schema-pattern schema)))
      (when (equal "" catalog)
	(setf catalog "NIL"))
      (let* ((wsdl-path (merge-pathnames 
			(string-downcase 
			 (format nil "infodb-~A-~A.wsdl" 
				 schema catalog))
			*wsdl-output-directory*))
	     (xsd-name (string-downcase 
			(format nil "infodb-~A-~A.xsd" 
				schema catalog)))
	     (xsd-path (merge-pathnames 
		       xsd-name
		       *wsdl-output-directory*)))
	(verbose "Writing WSDL to '~A'" wsdl-path)
	(persist-lxml (wsdl-instantiate wsdl :schema-location  xsd-name) wsdl-path)
	(verbose "Writing XML Schema to '~A'" xsd-path)
	(persist-lxml (xsd-instantiate xsd) xsd-path))))

(defun introspect-infodb (&key (schema "EUIDB") 
			  (catalogs '("USG_WARN_MAIL" "CALC_USAGE")))
  (initialize)
  (dolist (catalog catalogs)
    (introspect-catalog :catalog catalog :schema schema)))

(defun introspect-infodb-fully (&key (schema "EUIDB"))
  (initialize)
  (dolist (catalog
	    (get-unique-procedure-catalogs :schema schema))
    (verbose "Introspecting all procedures in '~A' catalog" catalog)
    (introspect-catalog :catalog catalog :schema schema)))

  